import { TableInfo } from "@/entities/tableInfo.entity";
import createFile from "./createFile";


/**
 * 创建控制器代码
 * @param name 
 * @param data 
 */
const createControllersFile = async (data: TableInfo) => {
    const { fieldInfoList = [], indexInfoList = [] } = data;
    //表名下划线转小驼峰
    const smallName = data.name.replace(/_(.)/g, (a: string, b: string) => `${b.toLocaleUpperCase()}`);

    //表名下划线转大驼峰
    const bigName = data.name.replace(/(?:^|_)(\w)/g, (_, c) => c.toUpperCase());

    const str = `
import { Context } from 'koa';
import { validateDto } from '@/utils/validateDto';
import { 
    Create${bigName}Dto, 
    Update${bigName}Dto, 
    Query${bigName}Dto, 
    Delete${bigName}Dto, 
    SelectOne${bigName}Dto,
    //Sort${bigName}Dto 
} from '@/dto/${smallName}.dto';
import ${smallName}Service from '@/services/${smallName}.service';

class ${bigName}Controller {
    async selectList(ctx: Context) {
        const reqData = ctx.request.body as Query${bigName}Dto;
        const errors = await validateDto(Query${bigName}Dto, reqData);
        if (errors) {
            ctx.body = errors;
            return
        }
        let resData = await ${smallName}Service.selectList(reqData)
        ctx.body = {
            code: 200,
            message: "列表查询成功",
            data:resData
        };
    }
    async selectOne(ctx: Context) {
        const reqData = ctx.request.body as SelectOne${bigName}Dto;
        let resData = await ${smallName}Service.selectOne(reqData)        
        ctx.body = {
            code: 200,
            message:"详情查询成功",
            data: resData,

        };
    }

    async add(ctx: Context) {
        const userId = ctx.state.userId as number;
        const reqData = ctx.request.body as Create${bigName}Dto;
        //reqData.userId = reqData.userId || userId; //暂时注释
        const errors = await validateDto(Create${bigName}Dto, reqData);
        if (errors) {
            ctx.body = errors;
            return
        }
        const resData = await ${smallName}Service.add(reqData);        
        ctx.body = {
        code: 200,
        message:"添加成功",
        data:{id:resData.id},
        };

    }

    async update(ctx: Context) {
        const reqData = <Update${bigName}Dto>ctx.request.body
        const errors = await validateDto(Update${bigName}Dto, reqData);
        if (errors) {
            ctx.body = errors;
            return
        }
        const resData = await ${smallName}Service.update(reqData);
        ctx.body = {
            code: 200,
            message:"更新成功",
            data: {},
        };

    }
    async delete(ctx: Context) {
        const reqData = ctx.request.body as Delete${bigName}Dto;
        const errors = await validateDto(Delete${bigName}Dto, reqData);
        if (errors) {
            ctx.body = errors;
            return
        }
        const resData = await ${smallName}Service.delete(reqData);
        ctx.body = {
            code: 200,
            message:"删除成功",
            data: {},
        };

    }
    /**
    async sort(ctx: Context) {
        const reqData = ctx.request.body as Sort${bigName}Dto;
        const errors = await validateDto(Sort${bigName}Dto, reqData);
        if (errors) {
            ctx.body = errors;
            return
        }
        const resData = await ${smallName}Service.sort(reqData);    
        ctx.body = {
            code: 200,
            message:"排序成功",
            data: {},
        };
    
    }*/

}

export default new ${bigName}Controller();`
    createFile(str, `controllers/${smallName}.controller.ts`)
    return str

}
export default createControllersFile;
